<template
  ><div>
    <PageWrapper :contentFullHeight="true" :contentBackground="true">
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="primary" @click="handleCreate"> {{ t('common.create') }} </a-button>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: t('common.confirmDelete'),
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
            :dropDownActions="[
              {
                label: t('routes.demo.FigureDocSettings.classProperty'),
                onClick: handleView.bind(null, record),
              },
              {
                label: t('routes.demo.FigureDocSettings.figureDocTemplate'),
                onClick: handleView2.bind(null, record),
              },
              {
                label: t('routes.demo.FigureDocSettings.extract'),
                onClick: handleView3.bind(null, record),
              },
              {
                label: t('routes.demo.FigureDocSettings.typeBind'),
                onClick: handleView4.bind(null, record),
              },
            ]"
          />
        </template>
      </BasicTable>
      <!-- 新增修改图文档弹窗 -->
      <DocSettingModal @register="registerModal" @success="reload" />
      <!-- 类属性弹窗 -->
      <ClassAttrModal @register="registerClassAttrModal" />
      <!-- 类型绑定弹窗 -->
      <TypeBindModal @register="registerTypeBindModal" />
      <!-- 图文档提取对应 -->
      <FigureDocExtractModal @register="registerFigureDocExtractModal" />
      <!-- 图文档模板 -->
      <DocTemplateModal @register="registerDocTemplateModal" />
    </PageWrapper>
  </div>
</template>

<script lang="ts" setup>
  import { useI18n } from '/@/hooks/web/useI18n';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { getFigureDocSettingsColumns } from './data';
  import { getAllFileTypeTable, deleteDocTypeData } from '/@/api/demo/system';

  import DocSettingModal from './DocSettingModal.vue';
  import ClassAttrModal from './ClassAttrModal.vue';
  import TypeBindModal from './TypeBindModal.vue';
  import FigureDocExtractModal from './FigureDocExtractModal.vue';
  import DocTemplateModal from './DocTemplateModal.vue';

  const { t } = useI18n();
  const { createMessage } = useMessage();

  // 新增
  const handleCreate = () => {
    openDocSettingModal(true, { isUpdate: false });
  };

  // 修改
  const handleEdit = (record: Recordable) => {
    openDocSettingModal(true, { isUpdate: true, currentRecord: record });
  };

  // 删除
  const handleDelete = (record: Recordable) => {
    deleteDocTypeData(record.id).then((res) => {
      if (res) {
        createMessage.success(t('common.delSuccessText'));
        reload();
      }
    });
  };

  // 查看类属性
  const handleView = (record: Recordable) => {
    openClassAttrModal(true, record);
  };

  // 查看图文档模板
  const handleView2 = (record: Recordable) => {
    openDocTemplateModal(true, record);
  };

  // 查看图文档对应提取
  const handleView3 = (record: Recordable) => {
    openFigureDocExtractModal(true, record);
  };

  // 查看类型绑定
  const handleView4 = (record: Recordable) => {
    openTypeBindModal(true, record);
  };

  // 初始化表格
  const [registerTable, { reload }] = useTable({
    api: getAllFileTypeTable,
    columns: getFigureDocSettingsColumns(),
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    actionColumn: {
      width: 80,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  // 新增修改弹窗
  const [registerModal, { openModal: openDocSettingModal }] = useModal();

  // 类属性弹窗
  const [registerClassAttrModal, { openModal: openClassAttrModal }] = useModal();

  // 类型绑定弹窗
  const [registerTypeBindModal, { openModal: openTypeBindModal }] = useModal();

  // 图文档提取对应弹窗
  const [registerFigureDocExtractModal, { openModal: openFigureDocExtractModal }] = useModal();

  // 图文档模板
  const [registerDocTemplateModal, { openModal: openDocTemplateModal }] = useModal();
</script>
